<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编号" prop="userNo">
        <el-input v-model="queryParams.userNo" placeholder="请输入编号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="案件简称" prop="caseName">
        <el-input v-model="queryParams.caseName" placeholder="请输入案件简称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="analysisStatus">
        <el-select v-model="queryParams.analysisStatus" placeholder="请选择状态" clearable>
          <el-option v-for="dict in analysis_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="材料上传状态 " prop="evidenceUpload">
        <el-select v-model="queryParams.evidenceUpload" placeholder="请选择材料上传状态 " clearable>
          <el-option v-for="dict in evidence_upload" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="案件阶段" prop="caseProcedure">
        <el-select v-model="queryParams.caseProcedure" placeholder="请选择案件阶段" clearable>
          <el-option v-for="dict in case_procedure" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="胜诉概率" prop="victoryRate">
        <el-input v-model="queryParams.victoryRate" placeholder="请输入胜诉概率" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['legal:cases:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['legal:cases:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['legal:cases:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['legal:cases:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="casesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="编号" align="center" prop="userNo" />
      <el-table-column label="对话id" align="center" prop="chatId" />
      <el-table-column label="案件简称" align="center" prop="caseName" />
      <el-table-column label="状态" align="center" prop="analysisStatus">
        <template #default="scope">
          <dict-tag :options="analysis_status" :value="scope.row.analysisStatus" />
        </template>
      </el-table-column>
      <el-table-column label="材料上传状态 " align="center" prop="evidenceUpload">
        <template #default="scope">
          <dict-tag :options="evidence_upload" :value="scope.row.evidenceUpload" />
        </template>
      </el-table-column>
      <el-table-column label="案件阶段" align="center" prop="caseProcedure">
        <template #default="scope">
          <dict-tag :options="case_procedure" :value="scope.row.caseProcedure" />
        </template>
      </el-table-column>
      <el-table-column label="胜诉概率" align="center" prop="victoryRate" />
      <el-table-column label="创案件建日期" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="文件内容" align="center" prop="caseContent" /> -->
      <el-table-column label="文件内容" align="center" prop="caseContent">
        <template #default="scope">
          <el-tooltip :content="scope.row.caseContent" placement="top"
            v-if="scope.row.caseContent && scope.row.caseContent.length > 20">
            <span>{{ scope.row.caseContent.substring(0, 20) }}...</span>
          </el-tooltip>
          <span v-else>{{ scope.row.caseContent }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['legal:cases:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['legal:cases:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改案件基本信息对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="casesRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="编号" prop="userNo">
          <el-input v-model="form.userNo" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="对话id" prop="chatId">
          <el-input v-model="form.chatId" placeholder="请输入对话id" />
        </el-form-item>
        <el-form-item label="案件简称" prop="caseName">
          <el-input v-model="form.caseName" placeholder="请输入案件简称" />
        </el-form-item>
        <el-form-item label="状态" prop="analysisStatus">
          <el-select v-model="form.analysisStatus" placeholder="请选择状态">
            <el-option v-for="dict in analysis_status" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="材料上传状态 " prop="evidenceUpload">
          <el-select v-model="form.evidenceUpload" placeholder="请选择材料上传状态 ">
            <el-option v-for="dict in evidence_upload" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="案件阶段" prop="caseProcedure">
          <el-select v-model="form.caseProcedure" placeholder="请选择案件阶段">
            <el-option v-for="dict in case_procedure" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="胜诉概率" prop="victoryRate">
          <el-input v-model="form.victoryRate" placeholder="请输入胜诉概率" />
        </el-form-item>
        <el-form-item label="文件内容">
          <editor v-model="form.caseContent" :min-height="192" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Cases">
import { listCases, getCases, delCases, addCases, updateCases } from "@/api/legal/cases"

const { proxy } = getCurrentInstance()
const { evidence_upload, analysis_status, case_procedure } = proxy.useDict('evidence_upload', 'analysis_status', 'case_procedure')

const casesList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userNo: null,
    caseName: null,
    analysisStatus: null,
    evidenceUpload: null,
    caseProcedure: null,
    victoryRate: null,
  },
  rules: {
    userNo: [
      { required: true, message: "编号不能为空", trigger: "blur" }
    ],
    chatType: [
      { required: true, message: "对话类型不能为空", trigger: "blur" }
    ],
    caseName: [
      { required: true, message: "案件简称不能为空", trigger: "blur" }
    ],
    analysisStatus: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
    evidenceUpload: [
      { required: true, message: "材料上传状态 不能为空", trigger: "change" }
    ],
    caseProcedure: [
      { required: true, message: "案件阶段不能为空", trigger: "change" }
    ],
    victoryRate: [
      { required: true, message: "胜诉概率不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询案件基本信息列表 */
function getList() {
  loading.value = true
  listCases(queryParams.value).then(response => {
    casesList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userNo: null,
    fileId: null,
    url: null,
    chatId: null,
    chatType: null,
    caseName: null,
    analysisStatus: null,
    evidenceUpload: null,
    caseProcedure: null,
    victoryRate: null,
    createTime: null,
    caseContent: null,
    updateTime: null
  }
  proxy.resetForm("casesRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加案件基本信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getCases(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改案件基本信息"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["casesRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCases(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addCases(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除案件基本信息编号为"' + _ids + '"的数据项？').then(function () {
    return delCases(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('legal/cases/export', {
    ...queryParams.value
  }, `cases_${new Date().getTime()}.xlsx`)
}

getList()
</script>
